package com.gxnu.edu.cn.client.controller;

import com.gxnu.edu.cn.ttxxt.entity.FileTransfers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.concurrent.CompletableFuture;

@Component
public class FileClient {
    @Autowired
    private RestTemplate restTemplate;

    private String baseUrl = "http://localhost:8080";

    public CompletableFuture<FileTransfers> uploadFile(HttpEntity<MultiValueMap<String, Object>> requestEntity) {
        return CompletableFuture.supplyAsync(() -> {
            try {
                ResponseEntity<FileTransfers> response = restTemplate.exchange(
                        baseUrl + "/api/files/upload",
                        HttpMethod.POST,
                        requestEntity,
                        FileTransfers.class
                );
                return response.getBody();
            } catch (Exception e) {
                throw new RuntimeException("文件上传失败: " + e.getMessage(), e);
            }
        });
    }

    public CompletableFuture<FileTransfers> getFileInfo(Long fileId) {
        return CompletableFuture.supplyAsync(() -> {
            try {
                ResponseEntity<FileTransfers> response = restTemplate.getForEntity(
                        baseUrl + "/api/files/" + fileId,
                        FileTransfers.class
                );
                return response.getBody();
            } catch (Exception e) {
                throw new RuntimeException("获取文件信息失败: " + e.getMessage(), e);
            }
        });
    }

    public CompletableFuture<List<FileTransfers>> getUserFiles(Long userId) {
        return CompletableFuture.supplyAsync(() -> {
            try {
                ResponseEntity<List> response = restTemplate.getForEntity(
                        baseUrl + "/api/files/user/" + userId,
                        List.class
                );
                return response.getBody();
            } catch (Exception e) {
                throw new RuntimeException("获取用户文件失败: " + e.getMessage(), e);
            }
        });
    }

    public CompletableFuture<Resource> downloadFile(Long fileId) {
        return CompletableFuture.supplyAsync(() -> {
            try {
                ResponseEntity<Resource> response = restTemplate.exchange(
                        baseUrl + "/api/files/download/" + fileId,
                        HttpMethod.GET,
                        null,
                        Resource.class
                );
                return response.getBody();
            } catch (Exception e) {
                throw new RuntimeException("下载文件失败: " + e.getMessage(), e);
            }
        });
    }
}